var RepairInfo = function (repair) {
  BaseModel.call(this);

  var partsPageSize = 8;

  var self = this;
  var fn = {
    initData: function (repair) {
      self.customerDimensions = ['name', 'monthCount', 'yearCount'];
      var customerRepairData = this.getCustomerRepairData(repair);
      var customerTextList = _.map(customerRepairData, 'id');
      self.customerRepairDataIds = customerTextList.join(',');
      self.customerRepairData = customerRepairData;

      self.partsDimensions = ['name', 'count', 'rate'];
      var partsRepairData = this.getPartsRepairData(repair);
      var partsTextList = _.map(partsRepairData, 'id');
      self.partsRepairDataIds = partsTextList.join(',');
      self.partsRepairData = partsRepairData;
      self.partsRepairIndex = 0;
    },

    getCustomerRepairData: function (repair) {
      var customerRepairData = {};
      var custYearList = repair.cus_year || [];
      var maxCustomerCount = 0;
      for (var i = 0; i < custYearList.length; i++) {
        var custYear = custYearList[i];
        var name = custYear.name;
        var count = custYear.count;
        if (maxCustomerCount < count) {
          maxCustomerCount = count;
        }

        customerRepairData[name] = {
          name: name,
          yearCount: count,
          monthCount: 0
        };
      }

      var custMonthList = repair.cus_month || [];
      for (var i = 0; i < custMonthList.length; i++) {
        var custMonth = custMonthList[i];
        var name = custMonth.name;
        var repairData = customerRepairData[name];
        if (repairData === undefined) {
          repairData = {
            name: name,
            yearCount: 0,
            monthCount: custMonth.count
          };
          customerRepairData[name] = repairData;
        } else {
          repairData.monthCount = custMonth.count;
        }
      }
      customerRepairData = _.values(customerRepairData);
      customerRepairData = _.orderBy(customerRepairData, ['yearCount'], ['desc']);
      for (var i = 0; i < customerRepairData.length; i++) {
        var obj = customerRepairData[i];
        obj.id = [obj.name, obj.yearCount, obj.monthCount].join('_')
      }

      maxCustomerCount = Math.ceil(_.round(maxCustomerCount / 5, 2)) * 5;
      self.maxCustomerCount = maxCustomerCount;
      return customerRepairData;
    },

    getPartsRepairData: function (repair) {
      var partList = repair.part || [];
      var sumCount = _.sumBy(partList, 'count');
      var maxPartsCount = 0;
      var partDataList = [];
      for (var i = 0; i < partList.length; i++) {
        var part = partList[i];
        var count = part.count;
        var rate = count / sumCount * 100;
        rate = _.round(rate, 2);

        if (rate < 1) {
          continue;
        }

        if (maxPartsCount < count) {
          maxPartsCount = count;
        }
        var name = part.name;
        partDataList.push({
          id: `${name}_${count}`,
          name: name,
          count: count,
          rate: rate
        });
      }

      partDataList = _.orderBy(partDataList, ['count'], ['desc']);

      self.maxPartsCount = Math.ceil(_.round(maxPartsCount / 5, 2)) * 5;
      return partDataList;
    }
  };

  this.incrementDayIndex = function () {
    var pageSize = partsPageSize;
    var totalCount = this.partsRepairData.length;
    if (totalCount <= pageSize) {
      return false;
    }

    var totalPage = Math.ceil(_.round(totalCount / pageSize, 2));
    var currentPage = this.partsRepairIndex / pageSize;
    currentPage = (currentPage + 1) % totalPage;
    this.partsRepairIndex = currentPage * pageSize;
    return true;
  };

  this.getCustomerChartData = function (pagination) {
    var currentPage = pagination.currentPage;
    var pageSize = pagination.pageSize;
    var startIndex = (currentPage - 1) * pageSize;

    var customerDimensions = this.customerDimensions;
    var customerRepairData = this.customerRepairData;
    customerRepairData = customerRepairData.slice(startIndex, startIndex + pageSize);
    customerRepairData = _.map(customerRepairData, function (data) {
      var values = [];
      for (var i = 0; i < customerDimensions.length; i++) {
        var dimension = customerDimensions[i];
        values.push(data[dimension]);
      }
      return values;
    });

    var len = customerRepairData.length;
    for (var i = len; i < pageSize; i++) {
      customerRepairData.push([i]);
    }

    return customerRepairData;
  };

  this.getPartsRepairData = function () {
    var pageSize = partsPageSize;
    var partsRepairData = this.partsRepairData;
    var partsRepairIndex = this.partsRepairIndex;
    partsRepairData = partsRepairData.slice(partsRepairIndex, partsRepairIndex + pageSize);

    var partsDimensions = this.partsDimensions;
    partsRepairData = _.map(partsRepairData, function (data) {
      var values = [];
      for (var i = 0; i < partsDimensions.length; i++) {
        var dimension = partsDimensions[i];
        values.push(data[dimension]);
      }
      return values;
    });

    var len = partsRepairData.length;
    for (var i = len; i < pageSize; i++) {
      partsRepairData.push([i]);
    }
    return partsRepairData;
  };

  this.resetInfo = function (repair) {
    var customerRepairData = fn.getCustomerRepairData(repair);
    var textList = _.map(customerRepairData, 'id');
    var customerRepairDataIds = textList.join(',');
    if (customerRepairDataIds !== this.customerRepairDataIds) {
      this.customerRepairDataIds = customerRepairDataIds;
      this.customerRepairData = customerRepairData;
    }

    var partsRepairData = fn.getPartsRepairData(repair);
    var partsTextList = _.map(partsRepairData, 'id');
    var partsRepairDataIds = partsTextList.join(',');
    if (partsRepairDataIds !== this.partsRepairDataIds) {
      this.partsRepairDataIds = partsRepairDataIds;
      this.partsRepairData = partsRepairData;

      var partsRepairIndex = this.partsRepairIndex;
      if (partsRepairIndex > partsRepairData.length) {
        this.partsRepairIndex = 0;
      }
    }
  };

  fn.initData(repair);
};
